package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.system.tools.BooleanMessage;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZhuanjiaDept;
import com.ruoyi.system.service.IZhuanjiaDeptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专家单位关系Controller
 * 
 * @author dy
 * @date 2024-05-29
 */
@Controller
@RequestMapping("/system/zhuanjiadept")
public class ZhuanjiaDeptController extends BaseController
{
    private String prefix = "system/zhuanjiadept";

    @Autowired
    private IZhuanjiaDeptService zhuanjiaDeptService;

    @RequiresPermissions("system:zhuanjiadept:view")
    @GetMapping()
    public String dept()
    {
        return prefix + "/dept";
    }

    /**
     * 查询专家单位关系列表
     */
    @RequiresPermissions("system:zhuanjiadept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZhuanjiaDept zhuanjiaDept)
    {
        startPage();
        List<ZhuanjiaDept> list = zhuanjiaDeptService.selectZhuanjiaDeptList(zhuanjiaDept);
        return getDataTable(list);
    }

    /**
     * 导出专家单位关系列表
     */
    @RequiresPermissions("system:zhuanjiadept:export")
    @Log(title = "专家单位关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhuanjiaDept zhuanjiaDept)
    {
        List<ZhuanjiaDept> list = zhuanjiaDeptService.selectZhuanjiaDeptList(zhuanjiaDept);
        ExcelUtil<ZhuanjiaDept> util = new ExcelUtil<ZhuanjiaDept>(ZhuanjiaDept.class);
        return util.exportExcel(list, "专家单位关系数据");
    }

    /**
     * 新增专家单位关系
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专家单位关系
     */
    @RequiresPermissions("system:zhuanjiadept:add")
    @Log(title = "专家单位关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZhuanjiaDept zhuanjiaDept)
    {
        return toAjax(zhuanjiaDeptService.insertZhuanjiaDept(zhuanjiaDept));
    }

    /**
     * 修改专家单位关系
     */
    @RequiresPermissions("system:zhuanjiadept:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZhuanjiaDept zhuanjiaDept = zhuanjiaDeptService.selectZhuanjiaDeptById(id);
        mmap.put("zhuanjiaDept", zhuanjiaDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存专家单位关系
     */
    @RequiresPermissions("system:zhuanjiadept:edit")
    @Log(title = "专家单位关系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZhuanjiaDept zhuanjiaDept)
    {
        return toAjax(zhuanjiaDeptService.updateZhuanjiaDept(zhuanjiaDept));
    }

    /**
     * 删除专家单位关系
     */
    @RequiresPermissions("system:zhuanjiadept:remove")
    @Log(title = "专家单位关系", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuanjiaDeptService.deleteZhuanjiaDeptByIds(ids));
    }

    @PostMapping("/findbydeptid")
    @ResponseBody
    public List<Map<String,Object>> findbydeptid(@RequestParam("deptid") String ppDeptId, @RequestParam("zhuanjianame")String ppZhuanjiaName)
    {
        ZhuanjiaDept mmDeptZhuanjia = new ZhuanjiaDept();
        mmDeptZhuanjia.setDeptid(ppDeptId);
        mmDeptZhuanjia.setXingming("%"+ppZhuanjiaName+"%");
        mmDeptZhuanjia.setStatus("1");
        List<Map<String,Object>> list = zhuanjiaDeptService.selectZhuanjiaDeptListWithoutDie(mmDeptZhuanjia);

        return list;
    }

    @PostMapping("/zhuanyi")
    @ResponseBody
    public JsonResult<?> zhuanyi(@RequestParam("zhuanjiaids")String ppZhuanjiaIds, @RequestParam("deptid")String ppDeptId){
        try{
            BooleanMessage bm = zhuanjiaDeptService.zhuanyi(ppZhuanjiaIds,ppDeptId);
            return bm.isOk()?JsonResult.getSuccessResult("转移成功"):JsonResult.getErrorResult(bm.getMessage().toString());
        }catch (Exception e){
            logger.error("zhuanjiadept -> zhuanyi: "+e.getMessage());
            return JsonResult.getErrorResult("转移专家失败");
        }
    }
}
